package com.smart.manager.work.repository;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Opt;
import com.smart.common.entities.constant.ArcoTips;
import com.smart.common.entities.core.Page;
import com.smart.manager.product.entities.models.BasicCamera;
import com.smart.manager.work.entities.body.RPigOrderFoods;
import com.smart.manager.work.entities.models.PigOrderFoods;
import com.smart.manager.work.entities.models.query.QPigOrderFoods;
import io.ebean.DB;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;


/**
 * 工单
 * @author leo
 * @date 2023/09/20
 */
@Service
@Log4j2
@RequiredArgsConstructor
public class PigOrderFoodsRepository {

    public Page<PigOrderFoods> pageList(RPigOrderFoods.SearchBody search) {
        QPigOrderFoods query = new QPigOrderFoods();
        Opt.ofBlankAble(search.getCode()).ifPresent(x -> query.code.contains(x));
        Opt.ofBlankAble(search.getKind()).ifPresent(x -> query.kind.eq(x));
        Opt.ofBlankAble(search.getStaff()).ifPresent(x -> query.staff.code.eq(x));
        Opt.ofBlankAble(search.getStatus()).ifPresent(x -> query.status.eq(x));
        query.order().id.desc();
        query.setFirstRow(search.firstRow()).setMaxRows(search.maxRows());
        return new Page<PigOrderFoods>().load(query.findPagedList());
    }

    public void add(RPigOrderFoods.EditBody body) {
        QPigOrderFoods query = new QPigOrderFoods();
        PigOrderFoods record = query.code.eq(body.getCode()).setMaxRows(1).findOne();
        Assert.isNull(record, "创建失败:, 工单代码已存在");
        BeanUtil.copyProperties(body, PigOrderFoods.class).save();
    }


    public void update(Long id, RPigOrderFoods.EditBody body) {
        PigOrderFoods record = getOneById(id);
        Assert.notNull(record, ArcoTips.UPDATE_NOT_FOUND);
        BeanUtil.copyProperties(body, record);
        Objects.requireNonNull(record).update();
    }



    /**
     * 根据id获取数据
     * @param id id
     * @return {@link BasicCamera}
     */
    private PigOrderFoods getOneById(Long id) {
        QPigOrderFoods query = new QPigOrderFoods();
        PigOrderFoods record = query.id.eq(id).setMaxRows(1).findOne();
        Assert.notNull(record, ArcoTips.UPDATE_NOT_FOUND);
        return record;
    }


    public int delByIds(List<Long> ids) {
        List<PigOrderFoods> list = new QPigOrderFoods().id.in(ids).findList();
        Assert.notEmpty(list, ArcoTips.DELETE_NOT_FOUND);
        for (PigOrderFoods item : list) {
            log.warn("判断数据是否能被删除");
        }
        return DB.deleteAll(list);
    }

}






